package com.ruoyi.project.gzbook.publishinfo.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.web.domain.Ztree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gzbook.publishinfo.domain.DgPublishInfo;
import com.ruoyi.project.gzbook.publishinfo.service.IDgPublishInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 出版社Controller
 * 
 * @author mhg
 * @date 2021-10-08
 */
@Controller
@RequestMapping("/gzbook/publishinfo")
public class DgPublishInfoController extends BaseController
{
    private String prefix = "gzbook/publishinfo";

    @Autowired
    private IDgPublishInfoService dgPublishInfoService;

    @RequiresPermissions("gzbook:publishinfo:view")
    @GetMapping()
    public String publishinfo()
    {
        return prefix + "/publishinfo";
    }

    /**
     * 查询出版社列表
     */
    @RequiresPermissions("gzbook:publishinfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DgPublishInfo dgPublishInfo)
    {
        startPage();
        List<DgPublishInfo> list = dgPublishInfoService.selectDgPublishInfoList(dgPublishInfo);
        return getDataTable(list);
    }

    /**
     * 导出出版社列表
     */
    @RequiresPermissions("gzbook:publishinfo:export")
    @Log(title = "出版社", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DgPublishInfo dgPublishInfo)
    {
        List<DgPublishInfo> list = dgPublishInfoService.selectDgPublishInfoList(dgPublishInfo);
        ExcelUtil<DgPublishInfo> util = new ExcelUtil<DgPublishInfo>(DgPublishInfo.class);
        return util.exportExcel(list, "出版社数据");
    }

    /**
     * 新增出版社
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出版社
     */
    @RequiresPermissions("gzbook:publishinfo:add")
    @Log(title = "出版社", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DgPublishInfo dgPublishInfo)
    {
        return toAjax(dgPublishInfoService.insertDgPublishInfo(dgPublishInfo));
    }

    /**
     * 修改出版社
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DgPublishInfo dgPublishInfo = dgPublishInfoService.selectDgPublishInfoById(id);
        mmap.put("dgPublishInfo", dgPublishInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存出版社
     */
    @RequiresPermissions("gzbook:publishinfo:edit")
    @Log(title = "出版社", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DgPublishInfo dgPublishInfo)
    {
        return toAjax(dgPublishInfoService.updateDgPublishInfo(dgPublishInfo));
    }

    /**
     * 删除出版社
     */
    @RequiresPermissions("gzbook:publishinfo:remove")
    @Log(title = "出版社", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dgPublishInfoService.deleteDgPublishInfoByIds(ids));
    }



    /**
     * 加载所有出版社列表树
     */
    @RequiresPermissions("gzbook:publishinfo:list")
    @GetMapping("/treePublish")
    @ResponseBody
    public List<Ztree> publishData()
    {
        DgPublishInfo query = new DgPublishInfo();
        List<DgPublishInfo> list = dgPublishInfoService.selectDgPublishInfoList(query);
        List<Ztree> ztrees = new ArrayList<>();
        for (DgPublishInfo pojo : list){
            Ztree ztree = new Ztree();
            ztree.setId(pojo.getId());
            ztree.setName(pojo.getPublishName()+ "&nbsp;&nbsp;&nbsp;" );
            ztree.setTitle(pojo.getPublishName());
            ztree.setpId(null);
            ztree.setChecked(false);
            ztree.setNocheck(false);
            ztree.setOpen(false);
            ztrees.add(ztree);
        }
        logger.info("出版社集合为--：" + ztrees);
        return ztrees;
    }



    /**
     * 选择出版社
     */
    @RequiresPermissions("gzbook:publishinfo:view")
    @GetMapping("/selectpublish/{publishId}")
    public String selectMenuTree( @PathVariable("publishId") Long publishId, ModelMap mmap)
    {
        logger.info("传过来的参数为publishId--："+ publishId);
        DgPublishInfo pojo = dgPublishInfoService.selectDgPublishInfoById(publishId);
        logger.info("查询结果--："+ pojo);
        mmap.put("publish", pojo);
        return prefix + "/tree";
    }


}
